package com.parkingspace.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.parkingspace.dao.ParkingInformationMapper;
import com.parkingspace.entity.ParkingInformation;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * ClassName: RabbitmqConfig
 * Package: com.parkingspace.config
 *
 * @author: guanzepeng
 * @Date: 2021/5/27 17:16 星期四
 * @Description:
 */
@Configuration
public class RabbitmqConfig {

	private final String dlxExchange = "sunspring_dlx_exchange";

	private final String dlxRoutingKey = "dlx";

	@Resource
	private ParkingInformationMapper parkingInformationMapper;

	/**
	 * 消费掉死信队列
	 * @param message
	 * @param channel
	 * @throws IOException
	 */
	@RabbitListener(queues = "sunspring.dlx.queue")
	public void dlxListener(Message message, Channel channel) throws IOException {
		String s = new String(message.getBody());
		System.out.println(s);

		QueryWrapper<ParkingInformation> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("parking_id",s);

		ParkingInformation parkingInformation = new ParkingInformation();
		parkingInformation.setCarUid(0L);
		parkingInformation.setParkingStatus(2);
		parkingInformation.setLicensePlateNumberEntering(0);

		parkingInformationMapper.update(parkingInformation, queryWrapper);

		//对消息进行业务处理....
		channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
	}

	/**
	 * 声明死信队列
	 * @return DirectExchange
	 */
	@Bean
	public DirectExchange dlxExchange() {
		return new DirectExchange(dlxExchange);
	}

	/**
	 * 声明死信队列
	 * @return Queue
	 */
	@Bean
	public Queue dlxQueue() {
		String dlxQueue = "sunspring.dlx.queue";
		return new Queue(dlxQueue);
	}

	/**
	 * 绑定死信队列到死信交换机
	 * @return Binding
	 */
	@Bean
	public Binding binding() {
		return BindingBuilder.bind(dlxQueue())
				.to(dlxExchange())
				.with(dlxRoutingKey);
	}

	/**
	 * 声明订单业务交换机
	 * @return DirectExchange
	 */
	@Bean
	public DirectExchange orderExchange() {
		String orderExchange = "sunspring_order_exchange";
		return new DirectExchange(orderExchange);
	}

	/**
	 * 声明订单业务队列
	 * @return Queue
	 */
	@Bean
	public Queue orderQueue() {
		Map<String,Object> arguments = new HashMap<>(2);
		// 绑定该队列到私信交换机
		arguments.put("x-dead-letter-exchange",dlxExchange);
		arguments.put("x-dead-letter-routing-key",dlxRoutingKey);
		String orderQueue = "sunspring_order_queue";
		return new Queue(orderQueue,true,false,false,arguments);
	}

	/**
	 * 绑定订单队列到订单交换机
	 * @return Binding
	 */
	@Bean
	public Binding orderBinding() {
		String orderRoutingKey = "sunspring.order";
		return BindingBuilder.bind(orderQueue())
				.to(orderExchange())
				.with(orderRoutingKey);
	}

}
